<template>
    <div>
        <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeSelect:changeSelect,changeTotalSize:changeTotalSize,events:events}">
        </iview-table>
        <br>
        <iview-page
                style="margin-bottom: 200px"
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
        <Modal v-model="showAdd" @on-ok="save" ok-text="保存" :closable='false' :mask-closable=true :width="800">
            <h3 slot="header" style="color:#2D8CF0">生活及护理建议</h3>
            <p align="center">
                <Transfer
                        :data="data"
                        :target-keys="keys"
                        :titles="['请选择建议','已选择建议']"
                        :list-style="listStyle"
                        :operations="['删除','添加']"
                        filterable
                        @on-change="handleChange"
                        @on-selected-change="selectedChange">
                    <div :style="{float: 'right', margin: '5px'}">
                        <Button type="ghost" size="small" @click="reload">刷新</Button>
                    </div>
                </Transfer>
            </p>
        </Modal>
        <Modal v-model="showModal1" @on-ok="addSupplement" ok-text="保存" :closable='false' :mask-closable=true :width="800">
            <h3 slot="header" style="color:#2D8CF0">补充建议</h3>
            <Row justify="center" class="code-row-bg">
                <Col span="24">
                    <Input v-model="supplementAdvice" :rows="20" :autosize="{minRows: 20}" type="textarea" placeholder="请输入内容..."></Input>
                </Col>
            </Row>
        </Modal>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage
        },
        data () {
            return {
                baseURL: '/user/life',
                showAdd: false,
                showModal1: false,
                selectRows: [],
                selectRow: {},
                supplementAdvice: '',
                formItem: {
                    id: '',
                    patientId: '',
                    complaintId: '',
                    doctorSuggestId: ''
                },
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/life',
                    defaultFilters: [
                        {'field': 'patient_id', 'value1': -1, 'operate': '=='},
                        {'field': 'complaint_id', 'value1': -1, 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 6300,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },{
                            title: '建议时间',
                            key: 'creationTime',
                            type: 'date',
                            sortable: true,
                            render: (h, params) => {
                                return h('span', this.$moment(params.row.creationTime).format('YYYY年MM月DD日'))
                            }
                        },{
                            title: '建议内容',
                            key: 'doctorSuggestId'
                        },{
                            title: '其他建议',
                            key: 'supplementAdvice'
                        },
                        {
                            title: '操作',
                            width: 200,
                            align: 'center',
                            render: (h, params) => {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.supplement(params.row)
                                            }
                                        }
                                    }, '补充')
                                ]);
                            }
                        }
                    ],
                    order: 'creation_time desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                },
                data: this.getData(),
                keys: this.getKeys(),
                listStyle: {
                    width: '44%',
                    height: '500px',
                    textAlign: 'left'
                }
            }
        },
        methods: {
            events (item) {
                if (item.path === 'add') {
                    this.add()
                } else if (item.path === 'edit') {
                    this.edit()
                } else if (item.path === 'delete') {
                    this.delete()
                } else if (item.path === 'print') {
                    this.print()
                }
            },
            supplement (row) {
                this.selectRow = row
                this.supplementAdvice = row.supplementAdvice
                this.showModal1 = true
            },
            addSupplement () {
                let url = this.baseURL + '/addSupplement'
                this.$ajax.post(url, {
                    id: this.selectRow.id,
                    supplementAdvice: this.supplementAdvice
                }).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '保存成功')
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            add () {
                this.getData()
                this.keys = []
                this.formItem.id = ''
                this.$bus.init(this.formItem)
                this.showAdd = true
            },
            edit () {
                this.data = []
                if (this.selectRows.length === 1) {
                    let url = this.baseURL + '/findById/' + this.selectRows[0].id
                    this.$ajax.get(url).then(r => {
                        let {code, params, msg} = r.data
                        if (code === 0) {
                            for (let row of params.datas) {
                                this.data.push({
                                    key: row.id,
                                    label: row.title
                                });
                            }
                            this.keys = JSON.parse(params.data.doctorSuggestId)
                            this.formItem = params.data
                            this.showAdd = true
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            delete () {
                if (this.selectRows.length > 0) {
                    let ids = []
                    for (let row of this.selectRows) {
                        ids.push(row.id)
                    }
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除'+ids.length+'条记录?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            doDelete (ids) {
                let url = this.baseURL + '/delete'
                this.$ajax.post(url, ids).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', '删除成功')
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            print (){
                if (this.selectRows.length === 1) {
                    let url = this.$store.state.app.WEBROOT + '/user/print/lifeCareAdvice?id=' + this.selectRows[0].id
                    window.open(url)
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            save () {
                if (this.formItem.id === '') {
                    this.formItem.patientId = this.$store.state.app.patientId
                    this.formItem.complaintId = this.$store.state.app.complaintId
                }
                this.formItem.doctorSuggestId = JSON.stringify(this.keys)
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '保存成功')
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            getData () {
                this.data = []
                let url = '/user/suggest/findAll'
                this.$ajax.get(url).then(r => {
                    let {code, data, msg} = r.data
                    if (code === 0) {
                        for (let row of data) {
                            this.data.push({
                                key: row.id,
                                label: row.title
                            });
                        }
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            getKeys () {
                return this.keys
            },
            handleChange (newTargetKeys) {
                this.keys = newTargetKeys;
            },
            selectedChange (sourceSelectedKeys, targetSelectedKeys) {
            },
            reload () {
                this.data = this.getData();
            },
            refresh () {
                if (this.$store.state.app.patientId) {
                    this.queryModel.defaultFilters[0] = {'field': 'patient_id', 'value1': this.$store.state.app.patientId, 'operate': '=='}
                }
                if (this.$store.state.app.complaintId) {
                    this.queryModel.defaultFilters[1] = {'field': 'complaint_id', 'value1': this.$store.state.app.complaintId, 'operate': '=='}
                }
                this.$refs.table.refresh()
            },
            changeSelect (selection) {
                this.selectRows = selection
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
                this.refresh()
            }
        },
        mounted () {
            // this.init()
        }
    }
</script>

<style>

</style>
